#ifndef _DDNS_PROTO_H_
#define _DDNS_PROTO_H_

#include "vpi.h"

#define DDNS_PROTO
#define DDNS_PROTO_TYPE  0x8001
#define DDNS_PROTO_NAME  "ddns"
#define DDNS_PROTO_VER   "1.7.0"

extern const sproto_header * ddns_default_header;

//=========================================================================
/*
 * protocol spec
 */
#define IS_DNS_UPCMD(cmd) ((cmd)>0xa0 && (cmd) < 0xa9)
#define IS_DNS_DOWNCMD(cmd) ((cmd)>0x00 && (cmd) < 0x10)

#define CLI_TOP_1			0x51
#define CLI_TOP_2			0x51
#define CLI_RESP			0xa1
#define CLI_UP_PW			0xa2
#define CLI_UP_IP			0xa3
#define CLI_UP_LOGIN		0xa4
#define CLI_UP_HEART		0xa5
#define CLI_UP_CHECKUPNP    0xa6
#define CLI_UP_ASKDOMAIN    0xa7
#define CLI_DEV_DBG 		0xa8
#define CLI_END				0x5A

#define SERV_TOP_1			0x5A
#define SERV_TOP_2			0x4D
#define SERV_REQ_TIME		0x01
#define SERV_GET_IP			0x02
#define SERV_REBOOT_DEV		0x03
#define SERV_SET_IP_PORT	0x04
#define SERV_RESP			0x05
#define SERV_GET_PW			0x06
#define SERV_ASK_UPNP       0x07
#define SERV_SET_DOMAIN     0x08
#define SERV_START_DBG		0x09
#define SERV_END			0x5A

#define CLI_RESP_OK				0x0
#define SERV_RESP_OK			0x0

typedef enum ddns_resp_result
{
    DDNS_RES_OK = 0,
    DDNS_RES_FAIL,
    DDNS_RES_ESUM, // checksum error
    DDNS_RES_EPARAM, // invalid params
    DDNS_RES_EOTH = 9 // other error
} eddns_resp_res;
typedef enum ddns_upnp_res
{
    DDNS_UPNP_OK = 0xa1,
    DDNS_UPNP_EACCESS = 0xa2,
    DDNS_UPNP_ERROR = 0xa3
} eddns_upnp_res;
//=========================================================================
// down cmd: server --> client
typedef union ddns_down_content
{
    unsigned int req_time;
    struct
    {
        char ip[16];
        unsigned int port;
    } set_ip_port;
    unsigned int resp_result;
    unsigned int upnp_result;
    char domain[32];
    unsigned int debug_flag;
} uddns_down_content;

// up cmd: client --> server
typedef struct ddns_up_ipinfo
{
    char id[12];
    char ip[16];
    unsigned int web_port;
    unsigned int stream_port;
} sddns_up_ipinfo;
typedef struct ddns_up_login
{
    char id[12];
    char ip[16];
    unsigned int web_port;
    unsigned int stream_port;
    char type[16];
    char version[16];
} sddns_up_login;
typedef struct ddns_up_heart
{
    char id[12];
    unsigned int web_port;
    unsigned int stream_port;
} sddns_up_heart;

typedef union ddns_up_content
{
    unsigned int resp_result;
    char login_pwd[16];
    struct ddns_up_ipinfo up_ipinfo;
    struct ddns_up_login up_login;
    struct ddns_up_heart up_heart;
    struct ddns_up_heart up_checkupnp;
    unsigned int up_askdomain;
    char dbg_devid[12];
} uddns_up_content;


typedef struct ddns_content
{
    unsigned char seq; // 流水号，上下行一致
    uddns_down_content down_content;
    uddns_up_content   up_content;
} sddns_content;

// 构建报文接口
int ddns_build_package(eproto_role role,sproto_msg *pmsg,unsigned char *sendbuff, int *len);
// 解析报文接口
int ddns_parse_package(eproto_role role,sproto_msg *pmsg, unsigned char *recvbuff, int len);

#endif

